package com.example.api2interface.utils;

import com.alibaba.fastjson.JSON;
import com.example.api2interface.entity.ResultData;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 返回json数据
 */
public class WebUtil {
    public static void writeJson(HttpServletResponse response, ResultData resultData) {
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        PrintWriter out = null;
        try {
            // 将Java对象转为JSON字符串
            String data = JSON.toJSONString(resultData);
            out = response.getWriter();
            out.print(data);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
